package com.zrrd.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

/**
 * @author WanJl
 * @version 1.0
 * @title T1
 * @description 使用Scanner、String类型的getBytes()方法、FileOutputStream。实现从控制台输入字符，存入指定的文件
 * @create 2024/5/19
 */
public class T1 {
    public static void main(String[] args){
        FileOutputStream fos =null;
        try {
            System.out.println("写点啥？");
            Scanner sc = new Scanner(System.in);
            fos = new FileOutputStream("E:/abc.txt", true);

            while (true) {
                String s = sc.nextLine();

                byte[] bytes = s.getBytes("UTF-8");
                //判断，如果是oevr，那么就关闭流，结束程序
                if (s.equals("over")) {
                    fos.close();
                    return;
                }
                fos.write(bytes);
                fos.write("\r\n".getBytes());//换行
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
